﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy
{
    public partial class _Default : System.Web.UI.Page
    {
        CDanhMuc cDanhMuc = new CDanhMuc();
        CBuyerOffer _cbo = new CBuyerOffer();
        CSellerOffer _cso = new CSellerOffer();
        CProduct _csp = new CProduct();
        CDanhMuc _cdm = new CDanhMuc();
        protected void Page_Load(object sender, EventArgs e)
        {
            gvCategories.DataSource = cDanhMuc.GetListCataLogIndustry();
            gvCategories.DataBind();
            Session["Flag"] = "";
            if (!IsPostBack)
            {
                LoadInFo();
            }
        }

        private void LoadInFo()
        {
            SellerOfferLatest();
            BuyerOfferLatest();
            ProductLatest();
            ProductFeatured();
            FeaturedCategories();
        }

        private void SellerOfferLatest()
        {
            List<BangTinBan> lists = _cso.GetTopTen();
            foreach (BangTinBan btm in lists)
            {
                Panel pn = new Panel();
                pn.ID = "pn" + btm.IDBangTinBan;
                pn.CssClass = "display_pn";

                Image img = new Image();
                img.ID = "img" + btm.IDBangTinBan.ToString();
                img.ImageUrl = "~/Images/Flags/" + btm.ThongTinTaiKhoan.QuocGia.TenQuocGia + ".gif";
                img.CssClass = "img_left";
                img.Height = 16;
                img.Width = 20;

                HyperLink a = new HyperLink();
                a.ID = "as" + btm.IDBangTinBan;
                a.CssClass = "a_left";
                a.Text = btm.TieuDe;
                a.NavigateUrl = "~/PageGuest/sSellOffersDetail.aspx?pid=" + btm.IDBangTinBan;

                Label lb = new Label();
                lb.ID = "lbs" + btm.IDBangTinBan;
                lb.CssClass = "label_right";
                lb.Text = btm.NgayDang + "<br />";

                pn.Controls.Add(img);
                pn.Controls.Add(a);
                pn.Controls.Add(lb);
                ol_selleroffer.Controls.Add(pn);
            }
        }

        private void BuyerOfferLatest()
        {
            List<BangTinMua> listb = _cbo.GetTopTen();
            
            foreach (BangTinMua btm in listb)
            {
                Panel pn = new Panel();
                pn.ID = "pn" + btm.IDBangTinMua.ToString();
                pn.CssClass = "display_pn";

                Image img = new Image();
                img.ID = "img" + btm.IDBangTinMua.ToString();
                img.ImageUrl = "~/Images/Flags/"+btm.ThongTinTaiKhoan.QuocGia.TenQuocGia+".gif";
                img.CssClass = "img_left";
                img.Height = 16;
                img.Width = 20;

                HyperLink a = new HyperLink();
                a.ID = "ab"+btm.IDBangTinMua.ToString();
                a.CssClass = "a_left";
                a.Text = btm.TieuDe;
                a.NavigateUrl = "~/PageGuest/bBuyOfferDetail.aspx?pid=" + btm.IDBangTinMua.ToString();

                Label lb = new Label();
                lb.ID = "lbb" + btm.IDBangTinMua.ToString();
                lb.CssClass = "label_right";
                lb.Text = btm.NgayDang+"<br />";

                pn.Controls.Add(img);
                pn.Controls.Add(a);
                pn.Controls.Add(lb);

                ol_buyeroffer.Controls.Add(pn);
            }
            
        }

        private void ProductLatest()
        {
            List<SanPham> list = _csp.GetTopTenLatest();
            dlLatestProducts.DataSource = list;
            dlLatestProducts.DataBind();
        }

        private void ProductFeatured()
        {
            List<SanPham> list = _csp.GetTopTenFeatured();
            dlFeaturedProducts.DataSource = list;
            dlFeaturedProducts.DataBind();
        }

        private void FeaturedCategories()
        {
            List<DanhMuc> list= _cdm.GetListFeatured();
            int i = 1;
            foreach (DanhMuc btm in list)
            {
                if (i <= 10)
                {
                    Panel pn = new Panel();
                    pn.ID = "pn" + btm.IDDanhMuc;
                    pn.CssClass = "display_pn";
                    HyperLink a = new HyperLink();
                    a.ID = "ab" + btm.IDDanhMuc;
                    a.CssClass = "a_left";
                    a.Text = btm.TenDanhMuc + "<br />";
                    a.NavigateUrl = "~/PageGuest/ListProductOfCategory.aspx?pid=" + btm.IDDanhMuc;
                    pn.Controls.Add(a);
                    ol_Catalogy1.Controls.Add(pn);
                }
                else
                {
                    Panel pn = new Panel();
                    pn.ID = "pn" + btm.IDDanhMuc;
                    pn.CssClass = "display_pn";
                    HyperLink a = new HyperLink();
                    a.ID = "ab" + btm.IDDanhMuc;
                    a.CssClass = "a_left";
                    a.Text = btm.TenDanhMuc + "<br />";
                    a.NavigateUrl = "~/PageGuest/ListProductOfCategory.aspx?pid" + btm.IDDanhMuc;
                    pn.Controls.Add(a);

                    ol_Catalogy2.Controls.Add(pn);
                }
                i++;
            }

        }

        public string CheckHinhAnh(object obj)
        {
            string strHinh = "";
            if (obj != null && !obj.ToString().Equals(""))
            {
                strHinh = "~/Images/Product/" + obj.ToString();
            }
            else
            {
                strHinh = "~/Images/Product/noimageavallable.jpg";
            }
            return strHinh;
        }
    }
}
